<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    var socket;
    //判断当前浏览器是否支持websocket编程
    if(window.WebSocket){
        socket = new WebSocket("ws://127.0.0.1:8090/socket/7");
        //相当于channel.read0,ev可以收到服务器端回送的消息
        socket.onmessage = function (ev) {
            var rt = document.getElementById("responseText");
            rt.value = rt.value + "\n" + ev.data;
        }
        //相当于连接开启,感知连接开启
        socket.onopen = function (ev) {
            var rt = document.getElementById("responseText");
            rt.value = "连接开启了..."
        }
        // 相当于连接关闭，感知连接关闭
        socket.onclose = function (ev) {
            var rt = document.getElementById("responseText");
            rt.value += "\n" + "连接关闭了..."
        }
    }else{
        alert("当前浏览器不支持webSocket编程");
    }

    //发送消息到服务器
    function send(msg){
        if(!window.socket){
            //判断socket是否创建好
            return;
        }
        if(socket.readyState == WebSocket.OPEN) {
            //通过socket发送消息
            socket.send(msg);
        }else{
            alert("连接未开启");
        }
    }
</script>

<form onsubmit="return fasle">
    <textarea name="msg" style="height:300px;width: 300px"></textarea>
    <input type="button" value="发送消息" onclick="send(this.form.msg.value)"></input>
    <textarea id="responseText" style="height:300px;width:300px"></textarea>
    <input type="button" value="清空" onclick="document.getElementById('responseText').value=''"/>

</form>

</body>
</html>